import { useCallback, useMemo } from 'react';
import * as logger from '../../logger';
import * as utils from './../utils';
/**
 * 查询
 */
var useFind = function useFind(treeNodes, options) {
  var _ref = options !== null && options !== void 0 ? options : {},
    _ref$uniqueKey = _ref.uniqueKey,
    uniqueKey = _ref$uniqueKey === void 0 ? 'key' : _ref$uniqueKey,
    _ref$childrenKey = _ref.childrenKey,
    childrenKey = _ref$childrenKey === void 0 ? 'children' : _ref$childrenKey,
    _ref$parentKey = _ref.parentKey,
    parentKey = _ref$parentKey === void 0 ? 'parentKey' : _ref$parentKey;
  var treeNodeMap = useMemo(function () {
    var result = {};
    utils.forEach(treeNodes, function (node) {
      result[node[uniqueKey]] = node;
    }, {
      childrenKey: childrenKey
    });
    return result;
  }, [treeNodes]);
  /** 通过 key 判断节点是否存在 */
  var hasNode = useCallback(function (key) {
    return !!treeNodeMap[key];
  }, [treeNodeMap]);
  /** 通过 key 获取节点 */
  var findNode = useCallback(function (key) {
    return treeNodeMap[key];
  }, [treeNodeMap]);
  /** 通过匹配条件获取节点 */
  var findBy = useCallback(function (cb) {
    return Object.keys(treeNodeMap).find(function (key) {
      return cb(treeNodeMap[key]);
    });
  }, [treeNodeMap]);
  /** 获取父节点 */
  var findParent = useCallback(function (node) {
    var parentUniqueKey = node[parentKey];
    return parentUniqueKey ? findNode(parentUniqueKey) : undefined;
  }, [parentKey, findNode]);
  /** 获取同一层级元素，包含自身 */
  var findSiblingsIncludeSelf = useCallback(function (node) {
    var parent = findParent(node);
    return parent ? parent[childrenKey] : treeNodes;
  }, [findParent, treeNodes]);
  /** 获取祖先节点 */
  var findAncestorNodes = useCallback(function (key) {
    if (!parentKey) {
      logger.fail('please config parentKey for getAncestorNodesByKey!');
    }
    var result = [];
    var currentNode = findNode(key);
    var getParent = function getParent(node) {
      var parentUniqueKey = node[parentKey];
      var parentNode = parentUniqueKey ? findNode(parentUniqueKey) : undefined;
      if (parentNode) {
        result.push(parentNode);
        getParent(parentNode);
      }
    };
    if (currentNode) getParent(currentNode);
    return result;
  }, [findNode]);
  /** 获取子孙节点 */
  var findPosterityNodesByKey = useCallback(function (key) {
    var result = [];
    var node = findNode(key);
    var getChildNodes = function getChildNodes(n) {
      var _n$childrenKey;
      (_n$childrenKey = n[childrenKey]) === null || _n$childrenKey === void 0 ? void 0 : _n$childrenKey.map(function (childNode) {
        result.push(childNode);
        getChildNodes(childNode);
      });
    };
    if (node) getChildNodes(node);
    return result;
  }, [findNode]);
  /** 找到节点在同一层级元素中的索引 */
  var findIndex = useCallback(function (node) {
    var siblings = findSiblingsIncludeSelf(node);
    return siblings.findIndex(function (child) {
      return node[uniqueKey] == child[uniqueKey];
    });
  }, [findSiblingsIncludeSelf]);
  return {
    treeNodeMap: treeNodeMap,
    hasNode: hasNode,
    findNode: findNode,
    findBy: findBy,
    findParent: findParent,
    findAncestorNodes: findAncestorNodes,
    findSiblingsIncludeSelf: findSiblingsIncludeSelf,
    findPosterityNodesByKey: findPosterityNodesByKey,
    findIndex: findIndex
  };
};
export default useFind;